'use strict';

const BaseService = require('./base');

class LibrariesService extends BaseService {
  constructor(ctx) {
    super(ctx, 'Libraries');
  }

  listOrder() {
    return [['id', 'ASC']];
  }

  pageOrder() {
    return [['id', 'DESC']];
  }

  async getPage(where, page, pageSize) {
    return this.ctx.model.Libraries.findAndCountAll({
      where,
      offset: pageSize * (page - 1),
      limit: pageSize,
      order: this.pageOrder(),
      include: [
        {
          model: this.ctx.model.Schools,
          as: 'school',
          attributes: ['id', 'name']
        }
      ]
    });
  }

  async findOne(id) {
    return await this.ctx.model.Libraries.findOne({
      where: {
        id,
      },
      include: [
        {
          model: this.ctx.model.Schools,
          as: 'school',
          attributes: ['id', 'name']
        }
      ]
    });
  }
}

module.exports = LibrariesService;
